package com.itlong.cloud.commons.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.operate.OperateLoggerOperationLogGetPageDTO;
import com.itlong.cloud.POJO.PO.OperationLogPO;
import com.itlong.cloud.POJO.VO.operate.OperateOperationLogVO;
import com.itlong.cloud.commons.dao.IOperationLogDao;
import com.itlong.cloud.commons.service.IOperationLogService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <desc>
 *      用户登录日志业务处理实现类
 * </desc>
 *
 * @createDate 2017/10/16
 */
@Service
public class OperationLogServiceImpl implements IOperationLogService{
    @Autowired
    IOperationLogDao iOperationLogDao;

    @Autowired
    MessageSender messageSender;

    /**
     * <desc>
     *      添加用户日志信息
     * </desc>
     *
     * @param operationLogPO 用户操作日志实体类
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperationLogPO operationLogPO) throws Exception {
        if (operationLogPO != null) {
            operationLogPO.setCreateTime(new Date());
            operationLogPO.setStatus(DataStatusEnum.NORMAL.getType());
            // 将要保存的操作日志发送到消息队列
            JSONObject dataJs = new JSONObject();
            dataJs.put("data",operationLogPO);
            messageSender.send(RabbitMQConstant.RABBITMQ_USER_OPERATION_LOG_SAVE_QUEUE,
                    JsonUtil.toJSON(dataJs), RabbitMQConstant.RABBITMQ_USER_EXCHANGE,
                    RabbitMQConstant.RABBITMQ_USER_OPERATION_LOG_SAVE_ROUTING_KEY);
            return 1;
        }
//        return iOperationLogDao.saveBuild(SqlUtil.durableData(operationLogPO, PlatformConstants.TABLE_SAVE));
        return null;
    }

    /**
     * <desc>
     *      保存开发者访问日志
     * </desc>
     *
     * @param map 日志信息
     * @return
     * @author wangzhi
     * @createDate 2018/12/19
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveDeveloperLog(Map<String, Object> map) throws Exception {
        if (map != null && !map.isEmpty()) {
            JSONObject dataJs = new JSONObject();
            dataJs.put("data",map);
            messageSender.send(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_LOG_SAVE_QUEUE,
                    JsonUtil.toJSON(dataJs), RabbitMQConstant.RABBITMQ_USER_EXCHANGE,
                    RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_LOG_SAVE_ROUTING_KEY);
            return 1;
        }
        return null;
    }

    /**
     * <desc>
     *      保存开发者异常日志
     * </desc>
     *
     * @param map 日志信息
     * @return
     * @author Qiang.S
     * @createDate 2019/09/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveDeveloperExceptionLog(Map<String, Object> map) throws Exception {
        if (map != null && !map.isEmpty()) {
            JSONObject dataJs = new JSONObject();
            dataJs.put("data",map);
            messageSender.send(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_EXCEPTION_LOG_SAVE_QUEUE,
                    JsonUtil.toJSON(dataJs), RabbitMQConstant.RABBITMQ_USER_EXCHANGE,
                    RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_EXCEPTION_LOG_SAVE_ROUTING_KEY);
            return 1;
        }
        return null;
    }

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @param pageDTO 查询参数DTO
     * @return page模型
     * @author Qiang.S
     * @createDate 2018/04/12
     */
    @Override
    public Page<OperateOperationLogVO> getPage(OperateLoggerOperationLogGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateOperationLogVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperationLogDao.getPage(params), iOperationLogDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      删除功能
     * </desc>
     *
     * @param ids 登录日志id数组
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2018/04/12
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delete(String ids){
        return iOperationLogDao.delete(StringHandlerUtil.splitString(ids),new Date());
    }
}
